package com.zhuss.controller.opus;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.zhuss.bean.bo.opus.opus.DetailOpusBo;
import com.zhuss.bean.bo.opus.opus.wechat.ListOpusBo;
import com.zhuss.bean.bo.opus.invit.DetailInvitBo;
import com.zhuss.bean.bo.opus.invit.wechat.ListInvitBo;
import com.zhuss.bean.vo.ListOpusTopicTreeVo;
import com.zhuss.bean.vo.ListInvitTopicTreeVo;
import com.zhuss.bean.vo.opus.opus.DetailOpusVo;
import com.zhuss.bean.vo.opus.invit.DetailInvitVo;
import com.zhuss.common.BaseController;
import com.zhuss.common.PageVo;
import com.zhuss.enums.BannerType;
import com.zhuss.exception.WpsException;
import com.zhuss.opus.invit.InvitReadServiceWechat;


/**
 * opus查询 controller
 * @Author zhushanshuo
   @Date 2020-02-04 22:58
 *
 */
  
 
@RestController
@RequestMapping("/opus")
public class OpusReadController extends BaseController {
	
	/*@Autowired
	@Qualifier("opusReadServiceWechat")
	private OpusReadServiceWechat opusReadServiceWechat;*/
	
	@Autowired
	@Qualifier("invitReadServiceWechat")
	private InvitReadServiceWechat invitReadServiceWechat;
	
	
	
	/**
	 * wechat
	 * 列表
	 * @param bo
	 * @return
	 * @throws WpsException
	 */
	@ResponseBody
    @RequestMapping("/listOpus")
	@SuppressWarnings("unchecked")
    public Object listOpus(@RequestBody ListOpusBo bo) throws WpsException {
		ListInvitBo param = new ListInvitBo();
		BeanUtils.copyProperties(bo, param);
		param.setInvitType(BannerType.作品.key());
		PageVo<DetailInvitVo> pageList = (PageVo<DetailInvitVo>) invitReadServiceWechat.listInvit(param);
		PageVo<DetailOpusVo> result = new PageVo<DetailOpusVo>();
		BeanUtils.copyProperties(pageList, result);
		List<DetailInvitVo> list = pageList.getItems();
		List<DetailOpusVo> items = new ArrayList<>();
		list.stream().forEach(new Consumer<DetailInvitVo>() {

			@Override
			public void accept(DetailInvitVo t) {
				DetailOpusVo item = new DetailOpusVo();
				BeanUtils.copyProperties(t, item);
				item.setOpusId(t.getInvitId());
				items.add(item);
				
			}
		});
		result.setItems(items);
		return result;
    }
	
	
	/**
	 * wechat
	 * 评论树
	 * 列表
	 * @param bo
	 * @return
	 * @throws WpsException
	 */
	@ResponseBody
	@RequestMapping("/listTopicTree")
	@SuppressWarnings("unchecked")
	public Object listTopicTree(@RequestBody DetailOpusBo bo) throws WpsException {
		DetailInvitBo param = new DetailInvitBo();
		BeanUtils.copyProperties(bo, param);
		param.setInvitId(bo.getOpusId());
		List<ListInvitTopicTreeVo> list = (List<ListInvitTopicTreeVo>) invitReadServiceWechat.listTopicTree(param);
		List<ListOpusTopicTreeVo> result = new ArrayList<ListOpusTopicTreeVo>();
		list.stream().forEach(new Consumer<ListInvitTopicTreeVo>() {

			@Override
			public void accept(ListInvitTopicTreeVo t) {
				ListOpusTopicTreeVo item = new ListOpusTopicTreeVo();
				BeanUtils.copyProperties(t, item);
				item.setOpusTopicId(t.getInvitTopicId());
				item.setChildren(genChildrenTree(t.getChildren()));
				result.add(item);
			}

		});
		return result;
	}
	
	protected List<ListOpusTopicTreeVo> genChildrenTree(
			List<ListInvitTopicTreeVo> children) {
		
		List<ListOpusTopicTreeVo> result = new ArrayList<ListOpusTopicTreeVo>();
		if (children == null || children.isEmpty()) {
			return result;
		}
		children.stream().forEach(new Consumer<ListInvitTopicTreeVo>() {

			@Override
			public void accept(ListInvitTopicTreeVo t) {
				ListOpusTopicTreeVo item = new ListOpusTopicTreeVo();
				BeanUtils.copyProperties(t, item);
				item.setOpusTopicId(t.getInvitTopicId());
				item.setChildren(genChildrenTree(t.getChildren()));
				result.add(item);
			}

		});
		return result;
	}
	
	
}
